package com.odm.communitymanage.ui.login

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import kotlinx.android.synthetic.main.fragment_search_community.*
import me.yokeyword.fragmentation.ISupportFragment


/**
 * @description: 查找小区 页面
 * @author: ODM
 * @date: 2020/4/16
 */
class SearchCommunityFragment : BaseMVVMFragment() {

    private lateinit var communityNames : MutableList<String>
    private lateinit var rvAdapter : CommunityNameAdapter

    init {
        //加载假数据 小区名
        initCommunityNames()
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {
        rvCommunityName.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = CommunityNameAdapter(communityNames)
        rvCommunityName.adapter = rvAdapter
        rvAdapter.setOnItemClickListener { adapter, view, position ->
            val selectedName = rvAdapter.data.get(position)
            //返回信息给 完善信息页
            setCommunityNameResult(selectedName)
        }
    }

    // 设置传给上个Fragment的数据
    private fun setCommunityNameResult(name : String) {
        val bundle = Bundle()
        bundle.putString(Constants.SELECTED_COMMUNITY_NAME, name)
        setFragmentResult(ISupportFragment.RESULT_OK , bundle)
        pop()
        hideSoftInput()
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_search_community
    }

    fun initCommunityNames() : MutableList<String> {
        communityNames = mutableListOf()
        communityNames.add("幸福社区1号")
        communityNames.add("幸福社区2号")
        communityNames.add("幸福社区3号")
        communityNames.add("幸福社区4号")
        communityNames.add("幸福社区5号")
        communityNames.add("慧智社区1号")
        communityNames.add("慧智社区2号")
        communityNames.add("慧智社区3号")
        communityNames.add("慧智社区4号")
        communityNames.add("慧智社区5号")
        communityNames.add("碧慧园1号")
        communityNames.add("碧慧园2号")
        communityNames.add("碧慧园3号")
        communityNames.add("碧慧园4号")
        communityNames.add("碧慧园5号")
        return communityNames
    }




    class CommunityNameAdapter (data : MutableList<String>)
        : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_community_name, data) {

        override fun convert(holder: BaseViewHolder, item: String) {
            holder.setText(R.id.tvCommunityName_item  , item)
        }
    }





}